//
// Created by zhupi on 2024/12/1.
//

#include "mutil_contens_dialog.h"
#include <QSplitter>
#include <QTextEdit>
#include <QVBoxLayout>


namespace
{
    const int QCOMBOX_WIDTH = 120;
}


void MutilContensDialog::Init()
{
    QVBoxLayout *mainVbox = new QVBoxLayout;
    MainWindow(mainVbox);
    this->setLayout(mainVbox);
}

MutilContensDialog::MutilContensDialog(const QString &title, QWidget *parent)
{

}

void MutilContensDialog::SetLeftData(const std::vector<std::pair<QString, QStringList>> &dataList)
{
    QStringList list;
    for (const auto &it : dataList) {
        // 将 QStringList 作为附加数据添加到 QListWidgetItem
        QListWidgetItem *item = new QListWidgetItem(it.first);
        item->setData(Qt::UserRole, QVariant::fromValue(it.second));  // 将 list1 存储为附加数据
        firstList_->addItem(item);
    }
}


void MutilContensDialog::InitRigth(QVBoxLayout *mainVbox)
{
    QLabel *label = new QLabel("fdsafdsa");
    mainVbox->addWidget(label);

    firstList_ = new  QListWidget(this);
    connect(firstList_, &QListWidget::currentItemChanged, this, &MutilContensDialog::LeftListClick);
    secondList_ = new  QListWidget(this);
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(firstList_);
    hbox->addWidget(secondList_);
    mainVbox->addLayout(hbox);

}

void MutilContensDialog::InitLeft(QVBoxLayout *mainVbox)
{
    QComboBox *combo1 = new QComboBox;
    combo1->setFixedWidth(QCOMBOX_WIDTH);
    mainVbox->addWidget(combo1);
}

void MutilContensDialog::MainWindow(QVBoxLayout *mainVbox)
{
    QWidget * window = new QWidget;
    QSplitter *splitter = new QSplitter(window);  // 创建一个分隔器

    // 创建两个控件
    QWidget *left = new QWidget;
    QWidget *rigth = new QWidget;

    QVBoxLayout * mainleftVbox = new QVBoxLayout;
    InitLeft(mainleftVbox);
    left->setLayout(mainleftVbox);


    QVBoxLayout * mainrigthVbox = new QVBoxLayout;
    InitRigth(mainrigthVbox);
    rigth->setLayout(mainrigthVbox);

    // 将控件添加到分隔器
    splitter->addWidget(left);
    splitter->addWidget(rigth);

//    // 设置窗口布局为 QSplitter
    mainVbox->addWidget(splitter);
}

void MutilContensDialog::LeftListClick(QListWidgetItem *current, QListWidgetItem *previous)
{
    QStringList list = current->data(Qt::UserRole).value<QStringList>();  // 获取附加数据
//
    secondList_->clear();
    for (const auto &it : list) {
        QListWidgetItem *item = new QListWidgetItem(it);
        secondList_->addItem(item);
    }
    qDebug() << "Selected List:" << list;
}
